package engine.ui.chat
{
	import engine.control.MenuList;
	import engine.data.ModelLocator;
	import engine.display.TalkManage;
	import engine.flag.GameDefine;
	import engine.logic.AlertLogic;
	import engine.logic.ChatFilterLogic;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import newx.com.config.StaticIni;
	
	public class ChatLogic extends EventDispatcher
	{
		private var 			model		:ModelLocator;
		private static 	var _self		:ChatLogic;
		public static 		var chatData	:Dictionary;
		public var channelStrList				:Array	=	[];
		public var channelData				:Array	=	[
			GameDefine.CHATTYPE_All,
			GameDefine.CHATTYPE_WORLD,
			GameDefine.CHATTYPE_WORLD_BY_MONEY,
			GameDefine.CHATTYPE_CAMP,
			GameDefine.CHATTYPE_ALLFRIEND,
			GameDefine.CHATTYPE_CASTLE_MASTER,
			GameDefine.CHATTYPE_CASTLE,
			GameDefine.CHATTYPE_GUILD,
			GameDefine.CHATTYPE_NATION,			
			GameDefine.CHATTYPE_BATTLECAMP,
			GameDefine.CHATTYPE_ALLFRIEND_ECHO,
			GameDefine.CHATTYPE_TEAM,
			GameDefine.CHATTYPE_GM_HELP,
			GameDefine.CHATTYPE_SCENE,
			GameDefine.CHATTYPE_TOGM
		];
		public var channelTypeData			:Array	=	[];
		public var chatFormat				:Array	=	[];
		
		private var _nxChatData				:Array	=	[];
		
		private var _currentChanel			:int	=	GameDefine.CHATTYPE_WORLD_BY_MONEY;
		private var _maxLength				:int	=	500;
		
		private var _channelGroup			:ChatChannelGroup;
		
		private var _systemFilters			:Array;
		private var _txt					:TextField;
		public static function getInstance():ChatLogic
		{
			if ( _self == null )
			{
				_self = new ChatLogic();
			}
			return _self;
		}
		
		public function ChatLogic(target:IEventDispatcher=null)
		{
			super(target);
			_txt = new TextField();
			//channelTypeData.push({label:StaticIni.getSwfLangStr("UIStr_1000047"),type:GameDefine.CHATTYPE_CAMP,enable:true,clickable:true});
			//channelTypeData.push({label:StaticIni.getSwfLangStr("RoleEquUI_Castle"),type:GameDefine.CHATTYPE_CASTLE,enable:true,clickable:true});
			channelTypeData.push({label:StaticIni.getSwfLangStr("Common_All"),type:GameDefine.CHATTYPE_All,enable:true,clickable:true});
			channelTypeData.push({label:StaticIni.getSwfLangStr("Channel_World"),type:GameDefine.CHATTYPE_WORLD_BY_MONEY,enable:true,clickable:true});
			channelTypeData.push({label:StaticIni.getSwfLangStr("Common_Home"),type:GameDefine.CHATTYPE_GUILD,enable:true,clickable:true});
//			channelTypeData.push({label:StaticIni.getSwfLangStr("Channel_Place"),type:GameDefine.CHATTYPE_SCENE,enable:true,clickable:true});
			channelTypeData.push({label:StaticIni.getSwfLangStr("UIStr_1000894"),type:GameDefine.CHATTYPE_GM_HELP,enable:true,clickable:true});
			//channelTypeData.push({label:StaticIni.getSwfLangStr("Channel_Team"),type:GameDefine.CHATTYPE_TEAM,enable:true,clickable:true});
//			channelTypeData.push({label:StaticIni.getSwfLangStr("Channel_System"),type:GameDefine.CHATTYPE_SYSTEM,enable:true,clickable:true});
//			channelTypeData.push({label:StaticIni.getSwfLangStr("UIStr_1000984"),type:GameDefine.CHATTYPE_BATTLECAMP,enable:false,clickable:false});
//			channelTypeData.push({label:StaticIni.getSwfLangStr("Channel_Speak"),type:GameDefine.CHATTYPE_WORLD,enable:false,clickable:false});
//			
//			channelStrList[GameDefine.CHATTYPE_WORLD]			=	StaticIni.getSwfLangStr("Channel_Speak");
			channelStrList[GameDefine.CHATTYPE_All]				=	StaticIni.getSwfLangStr("Common_All");
			channelStrList[GameDefine.CHATTYPE_WORLD_BY_MONEY]	=	StaticIni.getSwfLangStr("Channel_World");
			//channelStrList[GameDefine.CHATTYPE_CAMP]			=	StaticIni.getSwfLangStr("UIStr_1000047");
			//channelStrList[GameDefine.CHATTYPE_CASTLE]		=	StaticIni.getSwfLangStr("UIStr_1000287");
			channelStrList[GameDefine.CHATTYPE_GUILD]			=	StaticIni.getSwfLangStr("Common_Home");
			//channelStrList[GameDefine.CHATTYPE_BATTLECAMP]	=	StaticIni.getSwfLangStr("UIStr_1000984");
			//channelStrList[GameDefine.CHATTYPE_TEAM]			=	StaticIni.getSwfLangStr("Channel_Team");
//			channelStrList[GameDefine.CHATTYPE_GM_HELP]			=	StaticIni.getSwfLangStr("UIStr_1000894");
//			channelStrList[GameDefine.CHATTYPE_SCENE]			=	StaticIni.getSwfLangStr("Channel_Place");
			
			chatFormat[GameDefine.CHATTYPE_SCENE]	=	"Chatformat_Place";
//			chatFormat[GameDefine.CHATTYPE_TEAM]	=	"Chatformat_Team";
			chatFormat[GameDefine.CHATTYPE_GUILD]	=	"Chatformat_Guild";
			chatFormat[GameDefine.CHATTYPE_ALLFRIEND]	=	"Chatformat_Friend";
			chatFormat[GameDefine.CHATTYPE_ALLFRIEND_ECHO]	=	"Chatformat_FriendBack";
			chatFormat[GameDefine.CHATTYPE_CASTLE_MASTER]	=	"Chatformat_CastleMaster";
			chatFormat[GameDefine.CHATTYPE_CASTLE]	=	"Chatformat_Castle";
			chatFormat[GameDefine.CHATTYPE_WORLD]	=	"Chatformat_World";
			chatFormat[GameDefine.CHATTYPE_All]		=	"Chatformat_All";
			chatFormat[GameDefine.CHATTYPE_WORLD_BY_MONEY]	=	"Chatformat_WorldSpeak";
			chatFormat[GameDefine.CHATTYPE_WHISPER_ECHO]	=	"Chatformat_WhisperBack";
			chatFormat[GameDefine.CHATTYPE_WHISPER]	=	"Chatformat_Whisper";
			chatFormat[GameDefine.CHATTYPE_GM_HELP]	=	"Chatformat_GM";
			chatFormat[GameDefine.CHATTYPE_BATTLECAMP]	=	"Chatformat_BF";
			chatFormat[GameDefine.CHATTYPE_CAMP]	=	"Chatformat_Camp";
			chatFormat[GameDefine.CHATTYPE_TOGM]	=	"Chatformat_ToGM";
			
			model		=	ModelLocator.getInstance();
			chatData 	= new Dictionary();
			for(var i:uint=0;i<channelData.length;i++)
			{
				chatData[channelData[i]] = [];
			}
			
			_systemFilters = [	AlertLogic.TIPSTYPE_ERRORINFO,
				AlertLogic.TIPSTYPE_TIPSINFO,
				AlertLogic.TIPSTYPE_FIGHTINFO,
				AlertLogic.TIPSTYPE_FIGHT_CALC,
				AlertLogic.TIPSTYPE_CLONE];
			
			initEvent();
		}
		
		private function initEvent():void
		{
			addEventListener(ChatEvent.CHAT_CHANEL_SHOW, showChanel);
			addEventListener(ChatEvent.CHAT_SENDMSG, logMsg);
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_CHAT),getChat);
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_CASTLEBULLETIN), castleBulletin);
		}
		
		public function setBattleData(val:String):void
		{
			while(chatData[GameDefine.CHATTYPE_BATTLECAMP].length>_maxLength-1)
			{
				chatData[GameDefine.CHATTYPE_BATTLECAMP].shift();
			}
			chatData[GameDefine.CHATTYPE_BATTLECAMP].push(val);
		}
		
		private function getChat(e:CustomMsgEvent):void
		{
			var data	:Array	=	e.pList;
			//聊天类型
			var channelval	:int	=	data[0];
			var replaceArr:Array	=	[];
			
			var tStr:String				=	data[2];
			
			var	documentName:String;
			if(channelval == GameDefine.CHATTYPE_GM_HELP ||channelval==GameDefine.CHATTYPE_TOGM)
			{
				documentName ="filter_list1"
			}
			else 
			{
				documentName ="filter_list"
			}
			//过滤不能显示的文章
			var glStr:Array				=	StaticIni.getIniVar(documentName,"data").split(",");
			
			var tSize:int				=	glStr.length;
			
			if(channelval != GameDefine.CHATTYPE_TOGM
				&& channelval != GameDefine.CHATTYPE_SYSTEM&&channelval != GameDefine.CHATTYPE_GM_HELP)
			{
				if(tStr.indexOf("<") == -1)
				{
					tStr	=	ChatFilterLogic.getInstance().filter(tStr);
				}
			}
			
			data[2]			=	tStr;
			
			if(channelval==GameDefine.CHATTYPE_WORLD ||channelval==GameDefine.CHATTYPE_WORLD_BY_MONEY)
			{
				replaceArr[2]			=	data[1];
				var tackStara:String	=	data[2]
				replaceArr[3]	=	tackStara.slice(0,tackStara.length-1);
				var compStra:int	=	int(tackStara.charAt(tackStara.length-1));
				
				if(compStra	== 0){	
					
					replaceArr[1]	=	getWorldHeadStr();//"<font color='#216eef'>" + StaticIni.getSwfLangStr("UIStr_1001044") + "</font>"
				}else
				{
					replaceArr[1]	=	getWorldHeadStr();
				}
				
			}
			
			setChatData(data);
		}
		/**
		 *[世界] 
		 * @return 
		 * 
		 */		
		private function getWorldHeadStr():String
		{
			return 	StaticIni.getSwfLangStr("Channel_World")
		}
		private function logMsg(e:ChatEvent):void
		{
			var chatContent:String = e._data.toString();
			if(chatContent.indexOf("/")==0)
			{
				//找到第一个空格
				var index:int = chatContent.indexOf(" ");
				var chatName:String = chatContent.substr(1,index -1);
				var content:String = chatContent.substr(index + 1,chatContent.length);
				var tmpArr:Array	=	e._data.toString().split(" ");
				if(tmpArr.length>1)
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHAT,GameDefine.CHATTYPE_WHISPER,new WString(content),new WString(chatName));
				}
				else
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHAT,currentChannel,new WString(e._data as String));
				}
			}
			else
			{
				_currentChanel = currentChannel==GameDefine.CHATTYPE_All?GameDefine.CHATTYPE_WORLD_BY_MONEY:currentChannel;
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHAT,_currentChanel,new WString(e._data as String));
			}
			
		}
		
		private function showChanel(e:ChatEvent):void
		{
			MenuList.ShowMenu(channelData,changeChanel);
		}
		
		private function changeChanel(i:uint,data:Object=null):void
		{
			currentChannel	=	channelData[i];
		}
		
		public function get currentChannel():int
		{
			return _currentChanel;
		}
		
		public function set currentChannel(value:int):void
		{
			_currentChanel = value;
			dispatchEvent(new ChatEvent(ChatEvent.CHAT_CHANEL_CHANGE,value));
		}
		
		public function get currentChannelStr	():String
		{
			return channelStrList[_currentChanel];
		}
		
		public function clearData():void
		{
			for(var i:uint=0;i<channelData.length;i++)
			{
				chatData[channelData[i]] = [];
			}
			_nxChatData.length	=	0;
		}
		
		public function get nxChatData():Array
		{
			return _nxChatData;
		}
		
		public function addChatArr( value : Object ):Array
		{
			//聊天类型
			var chatType : int;
			//聊天内容
			var chatContent : String = "";
			//判断是否是无效的数据
			if( !( value is Array) )
			{
				chatType = value.type;
//				if(chatType == 0 && value.strID == "0" && value.msg == "0")
//				{//聊天频道 过段时间会出现一个0(服务端暂时没找到原因，先客户端屏蔽了)
//					return null;
//				}
				if( chatType == AlertLogic.TIPSTYPE_DEBUGINFO || chatType == AlertLogic.TIPSTYPE_FIGHTINFO)
				{
					return null;
				}
//				if(_channelGroup.channelTypeArr.indexOf(GameDefine.CHATTYPE_SYSTEM)!=-1)
				
				if( isFilter( chatType ))
				{
					value.sysId = 	chatType;
					_txt.htmlText = value.msg;
					chatContent	= StaticIni.getSwfLangStrVar("Chatformat_Person",[_txt.text])+"<br/>";
				}else
				{
					chatContent	= StaticIni.getSwfLangStrVar("Chatformat_System",[value.msg])+"<br/>";
				}
				
				if( chatType != AlertLogic.NumberTIPSTYPE_ACTIVITY_BROADCAST 
					&& chatType != AlertLogic.NumberTIPSTYPE_CHAT_BROADCAST
					&& chatType	!=	AlertLogic.TIPSTYPE_LEFTBOTTOM_TIPSINFO 	// 提示信息
					&& chatType	!=	AlertLogic.TIPSTYPE_LEFTBOTTOM_SYSTEM_BROADCAST		 // 系统广播
					&& chatType	!=	AlertLogic.TIPSTYPE_LEFTBOTTOM_ACTIVITY_BROADCAST
				)
				{
					chatType	=	GameDefine.CHATTYPE_SYSTEM;
				}
				
				value.type	=	chatType;
//				}
			}else
			{
				var replaceArr : Array	=	[];
				replaceArr[2]			=	value[1];
				replaceArr[3]			=	value[2];
				chatType				=	int(value[0]);
				
				if( chatType == GameDefine.CHATTYPE_WORLD || chatType == GameDefine.CHATTYPE_WORLD_BY_MONEY )
				{
					var tackStara:String	=	value[2];
					replaceArr[3]	=	tackStara.slice(0,tackStara.length-1);
					value[2]		=	replaceArr[3];
					
					var compStra:int	=	int(tackStara.charAt(tackStara.length-1));
					if(compStra	== 0){	
						replaceArr[1]	=	getWorldHeadStr()//"<font color='#216eef'>" + StaticIni.getSwfLangStr("UIStr_1001044") + "</font>"
					}else if(compStra	==	1)
					{
						replaceArr[1]	=	getWorldHeadStr();
					}
					chatContent = StaticIni.getSwfLangStrVar(chatFormat[chatType],replaceArr)+"<br/>";
				}
				else if(chatType == GameDefine.CHATTYPE_TOGM )
				{
					var name:String =model.scene.Role.getPlayerName();
					chatContent = StaticIni.getSwfLangStrVar(chatFormat[chatType],[name,replaceArr[2]])+"<br/>";
					return [chatType, chatContent];
				}
				else if( chatType == GameDefine.CHATTYPE_GM_HELP)
				{
					chatContent = StaticIni.getSwfLangStrVar(chatFormat[chatType],replaceArr)+"<br/>";
					return [chatType, chatContent];
				}
				else
				{
					chatContent = StaticIni.getSwfLangStrVar(chatFormat[chatType],replaceArr)+"<br/>";
				}
			}
			
			if( _nxChatData.length > _maxLength )
			{
				_nxChatData.shift();
			}
			
			if( value.sysId != null )
			{
				_nxChatData.push( [chatType, chatContent, value.sysId] ); 
			}else
			{
				
				_nxChatData.push( [chatType, chatContent] ); 
			}
			
			return _nxChatData[ _nxChatData.length - 1 ];	
		}
		
		/**
		 * 是否需要过滤
		 * @param type	信息类型
		 * @return 	是/否
		 * 
		 */
		private function isFilter( type : int ):Boolean
		{
			return -1 != _systemFilters.indexOf( type );
		}
		
		public function setChatData(value:Object):void
		{
			var chatArr : Array = addChatArr( value );
				
			var chatContent : String = chatArr[1];
			var chatType : int = chatArr[0];
			
//			if( value is Array )
//			{
//				chatType = value[0];
//			}else
//			{
//				chatType = value.type;
//			}
			 if(chatType == GameDefine.CHATTYPE_GM_HELP ||chatType == GameDefine.CHATTYPE_TOGM )
			{
				dispatchEvent(new ChatEvent(ChatEvent.CHAT_GETGMMSG,[chatType, chatContent]));
			}
			else if(chatContent != null && _channelGroup.channelTypeArr.indexOf(chatType)!=-1
			)
			{
				dispatchEvent(new ChatEvent(ChatEvent.CHAT_GETMSG,[chatType, chatContent]));
				TalkManage.getInstance().setTalk(value);
				//显示聊天气泡
			
			}
			else if( chatArr.length != 3 )
			{
//				dispatchEvent(new ChatEvent(ChatEvent.CHAT_GETMSG,[chatType, chatContent]));
			}
		}
		
		private function castleBulletin(e:CustomMsgEvent):void
		{
			var data		:Array		=	e.pList;
			var strID		:String		=	data[0];
			var tStr		:String		=	data[1];
			tStr	=	ChatFilterLogic.getInstance().filter(tStr);
			var obj			:Object 	= 	new Object();
			obj["type"] 	= 1;
			obj["strID"] 	= strID;
			obj["msg"]		= tStr;
			AlertLogic.getInstance().AddAlert(1,strID,[tStr]);
		}

		public function set channelGroup(value:ChatChannelGroup):void
		{
			_channelGroup = value;
		}

	}
}